﻿/*
=======================================================
    CheckWireropeStatusMonitor【在用绳检测状态】
=======================================================
*/

using NPOI.XWPF.UserModel;

namespace Services
{
    public interface ICheckWireropeStatusMonitorRepository : IRepository<CheckWireropeStatusMonitor>
    {
        List<CheckWireropeStatusMonitor> GetInUsedAll(int? _DepartmentID);
        List<CheckWireropeStatusMonitor> GetListByDepartmentIDs(string orderBy, int? top, params int[] departmentIDs);
        CheckWireropeStatusMonitor GetDataByDepartmentIDs(string orderBy, int sort, params int[] departmentIDs);
    }


    public class CheckWireropeStatusMonitorRepository : BaseRepository<CheckWireropeStatusMonitor>, ICheckWireropeStatusMonitorRepository
    {
        public CheckWireropeStatusMonitorRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<CheckWireropeStatusMonitor> query)
            : base(dB, currentUser, query)
        {
        }

        #region 获取在用绳的所有状态灯数据
        public List<CheckWireropeStatusMonitor> GetInUsedAll(int? _DepartmentID)
        {
            var exp = Expressionable.Create<CheckWireropeStatusMonitor, UsingWirerope>();
            exp = exp.And((cwsm, uw) => uw.Status == (int)UsingWirerope_Status.在用);
            if (_DepartmentID.HasValue && _DepartmentID.Value > 0)
            {
                exp = exp.And((cwsm, uw) => uw.DepartmentID == _DepartmentID.Value);
            }

            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                exp = exp.And((cwsm, uw) => cwsm.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                if (_currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item1 == false)
                {
                    exp = exp.And((cwsm, uw) => _currentUser.CurrentUserInfo.LoginUserKuangShanDepartment.Item2.Contains(uw.DepartmentID));
                }
            }
            else if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.监管部门)
            {
                exp = exp.And((cwsm, uw) => _currentUser.CurrentUserInfo.LoginRegulatorDepartmentIDs.Contains(uw.DepartmentID));
            }
            else
            {
                throw new CustomException("获取身份信息时出错！");
            }

            return _query.GetQueryable().InnerJoin<UsingWirerope>((cwsm, uw) => cwsm.UsingWireropeID == uw.UsingWireropeID)
                .Where(exp.ToExpression())
                .Select((cwsm, uw) => new CheckWireropeStatusMonitor { CheckWireropeStatusMonitorID = cwsm.CheckWireropeStatusMonitorID.SelectAll() })
                .ToList();
        }
        #endregion

        #region 钢丝绳综合状态排名
        public List<CheckWireropeStatusMonitor> GetListByDepartmentIDs(string orderBy, int? top, params int[] departmentIDs)
        {
            var _query = _DB.Queryable<CheckWireropeStatusMonitor, UsingWirerope>((x, uw) => new JoinQueryInfos(JoinType.Inner, x.UsingWireropeID == uw.UsingWireropeID))
                  .Where((x, uw) => departmentIDs.Contains(uw.DepartmentID) && x.SummaryVal.HasValue);

            if (orderBy.IsNotEmpty())
            {
                _query.OrderBy(orderBy);
            }

            if (top.HasValue && top.Value > 0)
            {
                _query.Take(top.Value);
            }

            return _query.Select((x, uw) => new CheckWireropeStatusMonitor
            {
                CheckWireropeStatusMonitorID = x.CheckWireropeStatusMonitorID.SelectAll(),
                UsingWireropeName = uw.Name,
                UsingWireropeCode = uw.Code,
            }).ToList();
        }
        #endregion

        public CheckWireropeStatusMonitor GetDataByDepartmentIDs(string orderBy, int sort, params int[] departmentIDs)
        {
            var _query = _DB.Queryable<CheckWireropeStatusMonitor, UsingWirerope>((x, uw) => new JoinQueryInfos(JoinType.Inner, x.UsingWireropeID == uw.UsingWireropeID))
                .Where((x, uw) => departmentIDs.Contains(uw.DepartmentID) && x.SummaryVal != null);

            if (orderBy.IsNotEmpty())
            {
                _query.OrderBy(orderBy);
            }

            return _query.Select((x, uw) => new CheckWireropeStatusMonitor
            {
                CheckWireropeStatusMonitorID = x.CheckWireropeStatusMonitorID.SelectAll(),
                UsingWireropeName = uw.Name,
                UsingWireropeCode = uw.Code,
            }).Skip(sort - 1).Take(1).First();
        }
    }
}
